package com.papers.juan.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.papers.common.annotation.Log;
import com.papers.common.annotation.RepeatSubmit;
import com.papers.common.core.controller.BaseController;
import com.papers.common.core.domain.PageQuery;
import com.papers.common.core.domain.R;
import com.papers.common.core.page.TableDataInfo;
import com.papers.common.core.validate.AddGroup;
import com.papers.common.core.validate.EditGroup;
import com.papers.common.enums.BusinessType;
import com.papers.common.utils.poi.ExcelUtil;
import com.papers.juan.domain.bo.TJuanGenerateDetailBo;
import com.papers.juan.domain.vo.TJuanGenerateDetailVo;
import com.papers.juan.service.ITJuanGenerateDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 组卷明细
 *
 * @author papers
 * @date 2022-11-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/juanGenerateDetail")
public class TJuanGenerateDetailController extends BaseController {

    private final ITJuanGenerateDetailService iTJuanGenerateDetailService;

    /**
     * 查询组卷明细列表
     */
    @SaCheckPermission("system:juanGenerateDetail:list")
    @GetMapping("/list")
    public TableDataInfo<TJuanGenerateDetailVo> list(TJuanGenerateDetailBo bo, PageQuery pageQuery) {
        return iTJuanGenerateDetailService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出组卷明细列表
     */
    @SaCheckPermission("system:juanGenerateDetail:export")
    @Log(title = "组卷明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TJuanGenerateDetailBo bo, HttpServletResponse response) {
        List<TJuanGenerateDetailVo> list = iTJuanGenerateDetailService.queryList(bo);
        ExcelUtil.exportExcel(list, "组卷明细", TJuanGenerateDetailVo.class, response);
    }

    /**
     * 获取组卷明细详细信息
     *
     * @param detailId 主键
     */
    @SaCheckPermission("system:juanGenerateDetail:query")
    @GetMapping("/{detailId}")
    public R<TJuanGenerateDetailVo> getInfo(@NotNull(message = "主键不能为空")
                                            @PathVariable String detailId) {
        return R.ok(iTJuanGenerateDetailService.queryById(detailId));
    }

    /**
     * 新增组卷明细
     */
    @SaCheckPermission("system:juanGenerateDetail:add")
    @Log(title = "组卷明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TJuanGenerateDetailBo bo) {
        return toAjax(iTJuanGenerateDetailService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改组卷明细
     */
    @SaCheckPermission("system:juanGenerateDetail:edit")
    @Log(title = "组卷明细", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TJuanGenerateDetailBo bo) {
        return toAjax(iTJuanGenerateDetailService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除组卷明细
     *
     * @param detailIds 主键串
     */
    @SaCheckPermission("system:juanGenerateDetail:remove")
    @Log(title = "组卷明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{detailIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] detailIds) {
        return toAjax(iTJuanGenerateDetailService.deleteWithValidByIds(Arrays.asList(detailIds), true) ? 1 : 0);
    }
}
